package com.ruoyi.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.labdata.service.EndTaskListener;
import com.ruoyi.system.domain.LabReceiptDetails;
import com.ruoyi.system.domain.Product;
import com.ruoyi.system.domain.ReceiptForm;
import com.ruoyi.system.service.ILabReceiptDetailsService;
import com.ruoyi.system.service.IProductService;
import com.ruoyi.system.service.IReceiptFormService;
@Component
public class ReceiptFormEndTaskListener extends EndTaskListener<ReceiptForm>{
	@Autowired
	private IReceiptFormService receiptFormService;
	@Autowired
	private IProductService productService;
	@Autowired
	private ILabReceiptDetailsService labReceiptDetailsService;
	@Override
	public int doTask(Long instanceId) {
		LabReceiptDetails queryDetail = new LabReceiptDetails();
		queryDetail.setInstanceId(instanceId);
		ReceiptForm form = labReceiptDetailsService.selectLabReceiptDetailsList(queryDetail);
		//form.setStatus("1");
		if(form.getReceiptTypeOpt()>4){
			return 1;
		}
		List<LabReceiptDetails> detailsList = form.getDetailsList();
		int i=1;
		for(LabReceiptDetails details : detailsList){
			Product product = new Product();
			product.setCollegeOrgId(form.getCollegeOrgId());
			product.setFundsItem(form.getFundsItem());
			product.setLabId(form.getLabId());
			product.setExpectUseOpt(form.getExpectUseOpt());
			product.setSpecs(details.getSpecs());
			product.setAssetName(details.getAssetName());
			product.setPurchaseDate(form.getPurchaseDate());
			product.setUnitPrice(details.getUnitPrice());
			product.setQuantity(details.getQuantity());
			product.setAvailableNum(details.getQuantity());
			product.setProductNo(form.getApplicationNo()+"_"+i);
			product.setInstanceId(instanceId);
			productService.insertProduct(product);
			i++;
		}
		//return receiptFormService.finishUpdate(instanceId);
		return detailsList.size();
	}

}
